package com.hw.interceptor;

import java.lang.reflect.Method;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hw.dagl.model.Sys_User;
import com.hw.dagl.model.Sys_User_Log;
import com.hw.dagl.service.SysUserLogService;

@Aspect
@Component
public class LogAspect2 {
	@Resource
	public SysUserLogService sysUserLogServiceImpl;
	
	private HttpServletRequest request=null;
	
	/*@Pointcut("@annotation(com.hw.interceptor.SystemLog)")
	public void serviceAspect(){
		
	}*/
	@Pointcut("execution(* com.hw.dagl.controller.*.*(..))")
	public void serviceAspect(){
		
	}
	@Before("serviceAspect()")
	public void before(JoinPoint joinPoint){
				
	}
	@After("serviceAspect()")
	public void after(JoinPoint joinPoint){
		request = getHttpServletRequest();
		Sys_User user = (Sys_User) request.getSession().getAttribute("user");
		String targetName=joinPoint.getTarget().getClass().getName();
		String methodName=joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();  
		//System.out.println(targetName+"=="+methodName+"=="+arguments[0]);
        Class targetClass = null;
        try {
            targetClass = Class.forName(targetName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }  
        Method[] methods = targetClass.getMethods();
        String operationName = "";
        String operationType = "";
        String Modelname = "";
        for (Method method : methods) {  
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();  
                if (clazzs!=null&&clazzs.length == arguments.length&&method.getAnnotation(SystemLog.class)!=null) {  
                    operationName = method.getAnnotation(SystemLog.class).operationName();
                    operationType = method.getAnnotation(SystemLog.class).operationType();
                    Modelname = method.getAnnotation(SystemLog.class).Modelname();
                  //  System.out.println(Modelname+operationType+operationName);
                    if (user != null && user.getYhdh() != null){
            			Sys_User_Log log = new Sys_User_Log();
            			log.setOptype(operationType);
            			log.setMethoddesc(operationName);
            			log.setModelname(Modelname);
            			log.setIp(user.getScdlip());
            			log.setUrl(request.getRequestURI());
            			log.setYhdh(user.getYhdh());
            			log.setDateTime(new Date());
            			// 异步保存日志
            			new SaveLogThread(log).start();
            		}
                    break;  
                }  
            }  
        }

	}
	
	/**
	 * 保存日志线程
	 */
	public class SaveLogThread extends Thread{
		
		private Sys_User_Log log;
		
		public SaveLogThread(Sys_User_Log log){
			super(SaveLogThread.class.getSimpleName());
			this.log = log;
		}
		
		@Override
		public void run() {
			sysUserLogServiceImpl.insertSysUserLog(log);
		}
	}
	/**
	 * 获取request实例
	 * @return
	 */
    public HttpServletRequest getHttpServletRequest(){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();  
        ServletRequestAttributes sra = (ServletRequestAttributes)ra;  
        HttpServletRequest request = sra.getRequest();
        return request;
    }
}
